package com.autonavi.notify.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.autonavi.notify.entity.Config;
import com.autonavi.notify.service.ConfigService;
import com.autonavi.notify.service.NotifyService;


@Controller
public class ConfigCtrl {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	ConfigService configService;
	
	@Autowired
	NotifyService notifyService;
	
	@RequestMapping(value = "/config/mail")
	@ResponseBody
	public Map<String,String> getConfigMail(){
		return configService.getMailConfig();
	}

	@RequestMapping(value = "/config/mail/save")
	@ResponseBody
	public void saveMailConfig(String smtpHost,String smtpPort,
			String smtpAccout,String smtpPassword,boolean useSSL){
		List<Config> confs = new ArrayList<Config>();
		confs.add(new Config("smtpHost",smtpHost));
		confs.add(new Config("smtpPort",smtpPort));
		confs.add(new Config("smtpAccout",smtpAccout));
		confs.add(new Config("smtpPassword",smtpPassword));
		confs.add(new Config("useSSL",String.valueOf(useSSL)));
		configService.save(confs);
	}
	
	@RequestMapping(value = "/config/mail/test")
	@ResponseBody
	public String mailTest(String smtpHost,String smtpPort,
			String smtpAccout,String smtpPassword,boolean useSSL){
		logger.info("receive message:{} {} {} {} {}",smtpHost,smtpPort,smtpAccout,smtpPassword,useSSL);
		try {
			notifyService.sendMail(smtpHost, smtpPort, useSSL, smtpAccout, smtpPassword, smtpAccout, "test", "test");
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			return e.toString();
		}
	}
	
	
}
